<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/3/10 14:39
 * Remark:
 */

namespace App\Http\Controllers\Contract;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Contract\AddRiskElementPost;
use App\Http\Requests\Contract\ListRiskElementGet;
use App\Service\Contract\RiskElementService;


class RiskElementController extends Controller
{
    private RiskElementService $_riskElementService;

    public function __construct()
    {
        $this->middleware([
            'riskElementLeg'
        ]);
        $this->_riskElementService = new RiskElementService();
    }

    /**
     * 新增风控要素
     * @param AddRiskElementPost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addRiskElement(AddRiskElementPost $request): \Illuminate\Http\JsonResponse
    {
        $data = $request->all();
        $res = $this->_riskElementService->addRiskElement($data);
        return ResponseFunc::returnJson(
            'error',
            [
                'risk_element_id' =>$res
            ],
            '新增合同风控要素成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    public function editRiskElement(int $riskElementId){
        $data = request()->post();
        $data['risk_element_id'] = $riskElementId;
        $this->_riskElementService->editRiskElement($data);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '修改合同风控要素成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    public function deleteRiskElement(int $riskElementId){
        $this->_riskElementService->deleteRiskElement($riskElementId);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '删除合同风控要素成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    public function getRiskElementList(ListRiskElementGet $request): \Illuminate\Http\JsonResponse
    {
        $pageSize = $request->get('page_size');
        $filters = $request->get('filters');
        return ResponseFunc::returnJson(
            'ok',
            $this->_riskElementService->getRiskElementList($pageSize,$filters),
            '获取风控要素列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function getRiskElementInfo(int $riskElementId){
        return ResponseFunc::returnJson(
            'error',
            $this->_riskElementService->getRiskElementInfo($riskElementId),
            '获取风控要素详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    public function getRiskElementSelectByContractType(int $contractTypeId){
        return ResponseFunc::returnJson(
            'error',
            $this->_riskElementService->getRiskElementSelectByContractType($contractTypeId),
            '获取风控要素下拉成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );

    }
}